<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>俱乐部</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            width: 60%;
            margin: 20px auto;
            text-align: center;
        }

        table {
            width: 100%;
            margin-top: 30px;
            border-collapse: collapse;
        }

        table caption {
            text-align: left;
            margin-bottom: 10px;
        }

        table tr td {
            border: 1px solid blue;
            padding: 10px 0px;
            text-align: center;
        }

        .small {
            width: 60px;
        }
    </style>
</head>

<body>
    <div>
        <!-- <caption>
            <button id="ascending-order" style="margin-left: 30px;" onclick="ascending()">升序排序</button>
            <button id="descending-order" style="margin-left: 30px;" onclick="descending()">降序排序</button>
        </caption> -->

        <table id="tb1">
            <tbody id="tbody">
                <tr>
                    <td onclick="sort()">编号</td>
                    <td>法定代表人</td>
                    <td>单位名称</td>
                </tr>
            </tbody>

        </table>
    </div>


    <div id="show"></div>
    <script src="club.js"></script>
    <script>
        let tbody = document.getElementById("tbody");
        for (let i = 0; i < data1.length; i++) {
            let tr = document.createElement('tr');
            let td1 = document.createElement('td');
            let td2 = document.createElement('td');
            let td3 = document.createElement('td');
            td1.innerHTML = data1[i].XH;
            td2.innerHTML = data1[i].FDDBR;
            td3.innerHTML = data1[i].DWMC
            tr.append(td1);
            tr.append(td2);
            tr.append(td3);
            tbody.append(tr);
        }

        // 升序排序
        // var ascending = document.getElementById('ascending-order');
        function ascending() {
            var table = document.getElementsByTagName('table')[0];
            var tr = table.getElementsByTagName('tr');
            var array = [];

            for (var i = 1; i < tr.length; i++) {
                // alert("aaa");
                array.push(tr[i]);
            }

            for (var i = 0; i < tr.length - 2 ; i++) {
                // alert("aaa");
                for (var j = 0; j < tr.length - i - 2; j++) {
                    // alert("aaa");
                    if (parseInt( array[j].getElementsByTagName('td')[0].innerText)  > parseInt(array[j + 1].getElementsByTagName('td')[0] 
                        .innerText)) {
                        var temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }

            // 输出
            for (var i = 0; i < array.length; i++) {
                // alert("aaa");
                table.append(array[i]);
            }
        }


        //降序排序
        function descending() {
            var table = document.getElementsByTagName('table')[0];
            var tr = table.getElementsByTagName('tr');
            var array = [];

            for (var i = 1; i < tr.length; i++) {
                // alert("aaa");
                array.push(tr[i]);
            }

            for (var i = 0; i < array.length - 1; i++) {
                for (var j = 0; j < array.length - i - 1; j++) {
                    if ( parseInt( array[j].getElementsByTagName('td')[0].innerText)  < parseInt(array[j + 1].getElementsByTagName('td')[0] 
                        .innerText)) {
                        var temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }

            // 输出
            for (var i = 0; i < array.length; i++) {
                // alert("aaa");
                table.append(array[i]);
            }
        }


        // 点击编号标签进行排序
        var t=1;
        function sort (){
            if(t){
                 ascending();
                 t=0;
            }else{
                 descending()
                 t=1;
            }
        }
        

    </script>


</body>

</html>